package com.weather;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class WeatherServlet extends HttpServlet {
	//private Timer timer1=new Timer();
	//private WeatherTask task1=new WeatherTask();
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		//timer1.schedule(task1, 0, 5*60*1000);
		String trigger=req.getParameter("trigger");
		if(trigger.equals("sample")){
			try {
				WeatherInfo weatherInfo=new GetWeatherInfo().getInfoFromAPI();
				Entity weather = new Entity("WeatherInfo");
				weather.setProperty("city", weatherInfo.getCity());
				weather.setProperty("cityid", weatherInfo.getCityid());
				weather.setProperty("temp", weatherInfo.getTemp());
				weather.setProperty("WD", weatherInfo.getWD());
				weather.setProperty("WS", weatherInfo.getWS());
				weather.setProperty("SD", weatherInfo.getSD());
				weather.setProperty("WSE", weatherInfo.getWSE());
				weather.setProperty("time", weatherInfo.getTime());
				weather.setProperty("timestamp", weatherInfo.getTimestamp());
				weather.setProperty("isRadar", weatherInfo.getIsRadar());
				weather.setProperty("Radar", weatherInfo.getRadar());
				DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
				datastore.put(weather);
				System.out.println("Data Stored");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(trigger.equals("day")){
			Entity weather = new Entity("WeatherInfoDay");
			Date d=new Date();
			long start=d.getTime();
			start=start-3600L*1000L*24;
			Query query = new Query("WeatherInfo").addFilter("timestamp", Query.FilterOperator.GREATER_THAN,start);
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
			int i;
			double sum_temp=0;
			double sum_hum=0;
			double sum_wind=0;
			for(i=0;i<=greetings.size()-1;i++){
				sum_temp+=Integer.parseInt(greetings.get(i).getProperty("temp").toString());
				sum_hum+=Integer.parseInt(greetings.get(i).getProperty("SD").toString().replace("%", ""));
				sum_wind+=Integer.parseInt(greetings.get(i).getProperty("WSE").toString());
			}
			sum_temp/=i;
			sum_hum/=i;
			sum_wind/=i;
			weather.setProperty("city", greetings.get(0).getProperty("city"));
			weather.setProperty("cityid", greetings.get(0).getProperty("cityid"));
			weather.setProperty("temp", sum_temp);
			weather.setProperty("SD", sum_hum);
			weather.setProperty("WSE", sum_wind);
			weather.setProperty("timestamp", d.getTime());
			datastore.put(weather);
			resp.getWriter().println(sum_temp+"\n"+sum_hum+"%\n"+sum_wind+"\n"+d+"\nis stored to WeatherInfoDay");
		}
		else if(trigger.equals("month")){
			Entity weather = new Entity("WeatherInfoMonth");
			Date d=new Date();
			if(d.getDate()==1){
				long start=d.getTime();
				start=start-3600L*1000L*24*30;
				Query query = new Query("WeatherInfoDay").addFilter("timestamp", Query.FilterOperator.GREATER_THAN,start);
				DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
				List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
				int i;
				double sum_temp=0;
				double sum_hum=0;
				double sum_wind=0;
				for(i=0;i<=greetings.size()-1;i++){
					sum_temp+=Double.parseDouble(greetings.get(i).getProperty("temp").toString());
					sum_hum+=Double.parseDouble(greetings.get(i).getProperty("SD").toString().replace("%", ""));
					sum_wind+=Double.parseDouble(greetings.get(i).getProperty("WSE").toString());
				}
				sum_temp/=i;
				sum_hum/=i;
				sum_wind/=i;
				weather.setProperty("city", greetings.get(0).getProperty("city"));
				weather.setProperty("cityid", greetings.get(0).getProperty("cityid"));
				weather.setProperty("temp", sum_temp);
				weather.setProperty("SD", sum_hum);
				weather.setProperty("WSE", sum_wind);
				weather.setProperty("timestamp", d.getTime()-3600L*24*1000L);
				datastore.put(weather);
				resp.getWriter().println(sum_temp+"\n"+sum_hum+"%\n"+sum_wind+"\n"+d+"\nis stored to WeatherInfoMonth");
			}
			
		}
		else if(trigger.equals("manual")){
			String table=req.getParameter("table");
			if(table.equals("WeatherInfoDay")||table.equals("WeatherInfoMonth")){
				Entity weather = new Entity(table);
				long start=Long.parseLong(req.getParameter("start"));
				long end=Long.parseLong(req.getParameter("end"));
				String source=req.getParameter("source");
				if(source!=null){
					Query query = new Query(source).addFilter("timestamp", Query.FilterOperator.GREATER_THAN,start);
					query.addFilter("timestamp", Query.FilterOperator.LESS_THAN, end);
					DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
					List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
					int i;
					double sum_temp=0;
					double sum_hum=0;
					double sum_wind=0;
					for(i=0;i<=greetings.size()-1;i++){
						sum_temp+=Integer.parseInt(greetings.get(i).getProperty("temp").toString());
						sum_hum+=Integer.parseInt(greetings.get(i).getProperty("SD").toString().replace("%", ""));
						sum_wind+=Integer.parseInt(greetings.get(i).getProperty("WSE").toString());
					}
					sum_temp/=i;
					sum_hum/=i;
					sum_wind/=i;
					weather.setProperty("city", greetings.get(0).getProperty("city"));
					weather.setProperty("cityid", greetings.get(0).getProperty("cityid"));
					weather.setProperty("temp", sum_temp);
					weather.setProperty("SD", sum_hum);
					weather.setProperty("WSE", sum_wind);
					weather.setProperty("timestamp",end);
					datastore.put(weather);
					resp.getWriter().println(sum_temp+"\n"+sum_hum+"%\n"+sum_wind+"\n"+end+"\nis stored to "+table);
				}
				
			}
			
		}
		resp.getWriter().println("Task Started");
	}
}
